/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tlf.dyjc5.servlet.system;

import com.google.gson.Gson;
import com.tlf.dyjc5.bo.MenuBean;
import com.tlf.dyjc5.service.MenuService;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dusin.web.util.JsonUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * @author dusin
 */
public class MenuAllLoad extends HttpServlet {

    MenuService menuService = null;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            long userId = (Long) request.getSession().getAttribute("userId");
            String username = (String) request.getSession().getAttribute("user");
            Map<String, Object> mapAllMenu = new HashMap<String, Object>();
            mapAllMenu.put("msg", "");
            mapAllMenu.put("code", 200);
            ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
            menuService = (MenuService) context.getBean("menuService");
            String currentMenuCode = request.getParameter("id");
            String user = (String) request.getSession().getAttribute("user");
            List<MenuBean> list = null;
            List<Map<String, Object>> mainMenu = new ArrayList<Map<String, Object>>();
            if ("super".equals(user)) {
                list = menuService.findAllDirectSubs("01", 0, 99);

            } else {
                list = menuService.findAllDirectSubsByUserId(userId, currentMenuCode);
            }
            for (MenuBean mb : list) {
                String menuCode = mb.getMenuCode();
                String url = mb.getUrl();
                Map<String, Object> menuItem = new HashMap<String, Object>();
                menuItem.put("id", mb.getId());
                menuItem.put("name", mb.getName());
                menuItem.put("menuCode", menuCode);
                menuItem.put("url", url);
                menuItem.put("icon", mb.getIconCls());
                if (!mb.isLeaf()) {
                    List<Map<String, Object>> subMenu = getSubMenu(userId, username, menuCode);
                    menuItem.put("subMenus", subMenu);
                } else {
                    menuItem.put("leaf", true);
                }
                mainMenu.add(menuItem);
            }

            mapAllMenu.put("data", mainMenu);
            String jsonText = null;
            Gson gson = new Gson();
            jsonText = gson.toJson(mapAllMenu);
            out.print(jsonText);
            out.flush();
        } finally {
            out.close();
        }
    }

    private List<Map<String, Object>> getSubMenu(long userId, String username, String superCode) {
        List<MenuBean> subMenuList = null;
        List<Map<String, Object>> subMenu = new ArrayList<Map<String, Object>>();
        if ("super".equals(username)) {
            subMenuList = menuService.findAllDirectSubs(superCode, 0, 99);

        } else {
            subMenuList = menuService.findAllDirectSubsByUserId(userId, superCode);
        }
        for (MenuBean mb : subMenuList) {
            String menuCode = mb.getMenuCode();
            String url = mb.getUrl();
            Map<String, Object> menuItem = new HashMap<String, Object>();
            menuItem.put("id", mb.getId());
            menuItem.put("name", mb.getName());
            menuItem.put("menuCode", menuCode);
            menuItem.put("url", url);
            menuItem.put("icon", mb.getIconCls());
            if (!mb.isLeaf()) {
                List<Map<String, Object>> subSubMenu = getSubMenu(userId, username, menuCode);
                menuItem.put("subMenus", subSubMenu);
            } else {
                menuItem.put("leaf", true);
            }
            subMenu.add(menuItem);
        }
        return subMenu;

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
